<?php
include("config.php");

session_start();
// if(!isset($_SESSION['user_id']))
// 	header('location:index.php');
if(!isset($_SESSION['username']))
	header('location:index.php');
error_reporting(0);

function get_sub_products($client, $sub_id, $languageId = 32){
	$result = array();
	if(strpos($sub_id, '-'))
	{
		$sub_id_explode = explode('-', $sub_id);
		foreach($sub_id_explode as $sub_id)
		{
			$parameters_sub_products = array('languageId'=>$languageId,'tecDocNodeId'=>$sub_id);
			$result[$sub_id] = $client->getArticleGroupTree($parameters_sub_products);
		}
	}
		
	else
	{
		$parameters_sub_products = array('languageId'=>$languageId,'tecDocNodeId'=>$sub_id);
		$result[$sub_id] = $client->getArticleGroupTree($parameters_sub_products);
	}
	
	return $result;
}

function createOptGroups($parent_id, $sub_products, $key_array, $label, &$output = '', $step = 0, $parents, $manufacturer_id){
	$step++;
	$optgroup_margin = 15 * $step;
	$output .= '<div id="'.$parent_id.'"><a href="'.$_SERVER['PHP_SELF'].'?id='.$_GET['id'].'&sub_id='.$parents.'&manufacturer_id='.$manufacturer_id.'" style="font-weight:bold;">-'.$label.'</a></div>';
	$output .= '<div id="group_'.$parent_id.'" style="margin-left:'.$optgroup_margin.'px;">';
	foreach($sub_products[$parent_id]->articleGroupTree as $key => $data)
	{

		$new_parents = $parents.'-'.$data->tecDocNodeId;
		if( (in_array($data->tecDocNodeId, $key_array)) && (!empty($sub_products[$data->tecDocNodeId])) )
			createOptGroups($data->tecDocNodeId, $sub_products, $key_array, $data->name, $output, $step, $new_parents, $manufacturer_id);
		else
			$output .= '<div id="'.$data->tecDocNodeId.'">+<a href="'.$_SERVER['PHP_SELF'].'?id='.$_GET['id'].'&sub_id='.$new_parents.'&manufacturer_id='.$manufacturer_id.'">'.$data->name.'</a></div>';

	}
	$output .= '</div>';

	return $output;
}

if( (isset($_GET['sub_id'])))
{
		if( strpos($_GET['sub_id'], '-') )
		{
			$sub_id_explode = explode('-', $_GET['sub_id']);
			$last_sub_id = end($sub_id_explode);
		}
}
else
	$last_sub_id = '';

?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="bg" lang="bg">
<head>
	<title><?php echo mb_convert_encoding('Вътрешна система за autohit','Windows-1251','UTF-8'); ?></title>
	<meta http-equiv="content-type" content="text/html; charset=Windows-1251" />

	<!-- Scripts -->
	<script type='text/javascript' src='js/scripts.js'></script>

	<!-- Style -->
	<link rel="stylesheet" type="text/css" href="css/styles.css" />
	<style>a{ text-decoration: none; color: black; }</style>
</head>
<body>
<?php
/**
	MANUFACTURY
*/
//default param
$languageId = 32;
$pathinfo = pathinfo($_SERVER['PHP_SELF']);
if(!isset($client))
{
	$options = array('languageId'=>$languageId,'trace' => 1,'login' => 'autohit2','password' => 'bont39er','encoding' => 'ISO-8859-1','trace' => true,'exceptions' => true);
	$client = new SoapClient('http://autohit.web-catalogue.eu/main/service/articlegroup.wsdl',$options);
}

$parameters_main_products = array('languageId'=>$languageId,'treeType'=>3,'engineId'=>$_GET['id']);
$result_main_products = $client->getFullArticleGroupTree($parameters_main_products); 

if(isset($_GET['sub_id']))
	$result_sub_products = get_sub_products($client, $_GET['sub_id']);

if($result_main_products)
{
	// All vehicle - select vehicle ENGINE
	foreach($_SESSION['vehicle'] as $key => $data)
	{
		if($data->id==$_GET['id']){
			if(is_numeric($key)){

				if($data->hpTo)
					$hp = $data->hpFrom.'-'.$data->hpTo;
				else
					$hp = $data->hpFrom;
					
				if($data->kwTo)
					$kw = $data->kwFrom.'-'.$data->kwTo;
				else
					$kw = $data->kwFrom;
					
				$manufacturerName = $data->manufacturerName;
				$description = $data->description;
			}
			else{
				if($_SESSION['vehicle']->hpTo)
					$hp = $_SESSION['vehicle']->hpFrom.'-'.$_SESSION['vehicle']->hpTo;
				else
					$hp = $_SESSION['vehicle']->hpFrom;
					
				if($_SESSION['vehicle']->kwTo)
					$kw = $_SESSION['vehicle']->kwFrom.'-'.$_SESSION['vehicle']->kwTo;
				else
					$kw = $_SESSION['vehicle']->kwFrom;
					
				$manufacturerName = $_SESSION['vehicle']->manufacturerName;
				$description = $_SESSION['vehicle']->description;
			}
			
			$_SESSION['label'] = $manufacturerName.' '.$description.' '.$kw.' '.$hp;
		}		
	}

?>
	<div style="font-size: 14px; font-weight: bold;">
		<?php echo $_SESSION['label']; ?>
	</div>


	<div id="mainContent">
<?php
	// Check is result
	$flag = 0;
	foreach($result_main_products->tree as $key => $data)
	{
		if( (!empty($result_sub_products)) && (isset($_GET['sub_id']))  && (is_array(array_keys($result_sub_products))) && (in_array($data->nodeId, array_keys($result_sub_products))) && (!empty($result_sub_products[$data->nodeId]))  )
		{
			$result = '';
			$result = createOptGroups($data->nodeId, $result_sub_products, array_keys($result_sub_products), $data->label, $reuslt, 0, $data->nodeId, $_GET['manufacturer_id']);
			echo $result;
		}
		else
		{
			if( (!empty($_GET['id'])) && (!empty($data->nodeId)) && (!empty($data->label)) ){
				$flag = 1;
?>
				<div id="<?php echo $data->nodeId;?>"><a href="<?php echo $_SERVER['PHP_SELF'];?>?id=<?php echo $_GET['id'];?>&sub_id=<?php echo $data->nodeId;?>&manufacturer_id=<?php echo $_GET['manufacturer_id']; ?>">+<?php echo $data->label; ?></a></div>
<?php
			}
		}
	}

	if(empty($flag)){
?>
		<div class="no_result"><?php echo mb_convert_encoding('Няма намерени резултати','Windows-1251','UTF-8'); ?></div>
<?php
	}
?>
	</div>

	<div id="product_manufacturer">
<?php
		if( $last_sub_id && isset($_GET['id']) && $_GET['id'] )
		{
			$parameters_articles = array('languageId'=>32,'treeType'=>1,'vehicleTypeId'=>$_GET['id'],'nodeId'=>$last_sub_id,'own'=>0);
			$result_article = $client->getArticleGroupsDealersForNode($parameters_articles);
			if(!is_array($result_article->articleGroupsDealers))
				$result_article->articleGroupsDealers = array('0' => $result_article->articleGroupsDealers);
			foreach($result_article->articleGroupsDealers as $dealer)
			{
				if( isset($dealer->dealerId) )
				{
					if(!isset($client_articleGroupsDealers))
					{
						$options = array('languageId'=>$languageId,'trace' => 1,'login' => 'autohit2','password' => 'bont39er','encoding' => 'ISO-8859-1','trace' => true,'exceptions' => true);
						$client_articleGroupsDealers = new SoapClient('http://autohit.web-catalogue.eu/main/service/article.wsdl',$options);
					}
					$parameters_articleGroupsDealers = array('languageId'=>$languageId,'own'=>0,'vehicleTypeId'=>0+$_GET['id'],'genartIdTecDoc'=>array(0+$dealer->articleGroupId),'dealerIdTecDoc'=>array(0+$dealer->dealerId),'vehicleType'=>2);
					$result_articleGroupsDealers = $client_articleGroupsDealers->searchArticleByArticleGroup($parameters_articleGroupsDealers);
					$results_articleGroupsDealers[$dealer->articleGroupId.'_'. $dealer->dealerId] = $result_articleGroupsDealers;
					if( (isset($result_articleGroupsDealers->article)) )
					{
				
?>
						<div style="font-size:14px;">
							<a onClick="ajaxLoadHistoryEngine(<?php echo $_GET['manufacturer_id']; ?>, 0,<?php echo $_GET['id']; ?>);" href="result.php?id=<?php echo $dealer->articleGroupId.'&dealer_id='.$dealer->dealerId.'&manufacturer_id='.$_GET['manufacturer_id']; ?>" target="_blank" style="font-weight:bold;"><?php echo $dealer->dealerName;?>
								<div style="font-style:italic;font-size:12px;float:right;">(<?php echo $dealer->articleGroupName; ?>)</div>
							</a>
						</div>
<?php
					}
				}
			}
		}
?>
	</div>
	<div style = "clear:both;"></div>

<?php
	if(isset($results_articleGroupsDealers)){
		$_SESSION['articleGroupsDealers'] = $results_articleGroupsDealers;
	}
}
?>
</body>
</html>